package com.yyz.service.impl;

import com.yyz.mapper.ContractMapper;
import com.yyz.pojo.Contract;
import com.yyz.service.ContractService;
import com.yyz.tools.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractMapper contractMapper;

    public Page<Contract> findByPage(Map<String, Object> map) {
        //查找数据中记录总数中，count(1) 和limit配合
        // sql语句将返回空行，因为count(1)只会使sql语句产生一条结果记录，
        // 所以后面的limit 1,5 是完全找不到的。
        Integer count = contractMapper.countByQuery(map);    //查找表中的总量
        Page<Contract> pa = new Page<Contract>();
        pa.setCount(count);
        pa.setIndex(Integer.parseInt(map.get("index").toString()));
        map.put("index", (pa.getIndex() - 1)*pa.getSize());
        map.put("size", pa.getSize());
        List<Contract> list = contractMapper.findByQuery(map);
        pa.setList(list);
        return pa;
    }

    public Integer countByQuery(Map<String, Object> map) {
        return contractMapper.countByQuery(map);
    }

    public Integer delete(Integer id) {
        return contractMapper.delete(id);
    }

    public Integer add(Contract contract) {
        return contractMapper.add(contract);
    }

    public Integer update(Contract contract) {
        return contractMapper.update(contract);
    }

    public Contract queryById(Integer id) {
        return contractMapper.queryById(id);
    }
}

